{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Jewels and Stones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numJewelsInStones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #宝石与石头"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>&nbsp;给你一个字符串 <code>jewels</code>&nbsp;代表石头中宝石的类型，另有一个字符串 <code>stones</code> 代表你拥有的石头。&nbsp;<code>stones</code>&nbsp;中每个字符代表了一种你拥有的石头的类型，你想知道你拥有的石头中有多少是宝石。</p>\n",
    "\n",
    "<p>字母区分大小写，因此 <code>\"a\"</code> 和 <code>\"A\"</code> 是不同类型的石头。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>jewels = \"aA\", stones = \"aAAbbbb\"\n",
    "<strong>输出：</strong>3\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>jewels = \"z\", stones = \"ZZ\"\n",
    "<strong>输出：</strong>0<strong>\n",
    "</strong></pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;=&nbsp;jewels.length, stones.length &lt;= 50</code></li>\n",
    "\t<li><code>jewels</code> 和 <code>stones</code> 仅由英文字母组成</li>\n",
    "\t<li><code>jewels</code> 中的所有字符都是 <strong>唯一的</strong></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [jewels-and-stones](https://leetcode.cn/problems/jewels-and-stones/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [jewels-and-stones](https://leetcode.cn/problems/jewels-and-stones/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"aA\"\\n\"aAAbbbb\"', '\"z\"\\n\"ZZ\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        if not J:\n",
    "            return 0\n",
    "        \n",
    "        cMap = Counter(S)\n",
    "        \n",
    "        total = 0\n",
    "        for a in J:\n",
    "            total += cMap.get(a, 0)\n",
    "            \n",
    "        return total\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        num=0\n",
    "        for j in J:\n",
    "            n=S.count(j)\n",
    "            num+=n\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        js = {c for c in J}\n",
    "        return sum((1 for c in S if c in js))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        v = set(J)\n",
    "        return sum([1 for x in S if x in v])\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        result = 0\n",
    "        for i in S:\n",
    "            if i in J:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        temp = 0\n",
    "        for s in S:\n",
    "            if s in J:\n",
    "                temp += 1\n",
    "        return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        v = set(J)\n",
    "        return sum([1 for x in S if x in v])\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        return sum(map(J.count, S))\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J, S):\n",
    "        \"\"\"\n",
    "        :type J: str\n",
    "        :type S: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # Solution 1:\n",
    "        if not J or not S:\n",
    "            return 0\n",
    "        v = set(J)\n",
    "        return sum([1 for x in S if x in v])\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: 'str', S: 'str') -> 'int':\n",
    "        count = 0\n",
    "        for s in S:\n",
    "            if s in J:\n",
    "                count += 1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        count = 0\n",
    "        for n in J:\n",
    "            for m in S:\n",
    "                if n == m:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        n = 0\n",
    "        for i in J:\n",
    "            if i in S:\n",
    "                n += S.count(i)\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        result = 0\n",
    "        for i in S:\n",
    "            if i in J:\n",
    "                result = result +1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        num = 0\n",
    "        for s in S:\n",
    "            if s in J:\n",
    "                num+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        d = {}\n",
    "        for s in S:\n",
    "            d[s] = d[s] + 1 if s in d else 1\n",
    "        sum = 0\n",
    "        for j in J:\n",
    "            sum += d[j] if j in d else 0 \n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        count = 0\n",
    "        for i in S:\n",
    "            if i in J:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        counts = 0 \n",
    "        J_list = list(J)\n",
    "        for i in S:\n",
    "            if i in J_list:\n",
    "                counts += 1\n",
    "        return counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        nums = 0\n",
    "        for i in J:\n",
    "            nums += S.count(i)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        jewelsSet = set(J)\n",
    "        return sum(s in jewelsSet for s in S)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        count = 0\n",
    "        for s in S:\n",
    "            print('s--', s)\n",
    "            for j in J:\n",
    "                print('j--', j)\n",
    "                if s==j:\n",
    "                    count += 1\n",
    "                    print(count)\n",
    "                    break\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, J: str, S: str) -> int:\n",
    "        a = 0\n",
    "        for i in J:\n",
    "            a += S.count(i)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        d={}\n",
    "        sum1=0\n",
    "        for i in stones:\n",
    "            if i not in d:\n",
    "                d[i]=1\n",
    "            else:\n",
    "                d[i]=d[i]+1\n",
    "        for i in jewels:\n",
    "            if i in stones:\n",
    "                sum1=sum1+d[i]\n",
    "        return sum1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        k = 0\n",
    "        for i in stones:\n",
    "            if i in jewels:\n",
    "                k = k + 1\n",
    "        return k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        temp = set(jewels)\n",
    "        ans = 0\n",
    "        for s in stones:\n",
    "            if s in temp:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\r\n",
    "        \r\n",
    "        \r\n",
    "        res = 0\r\n",
    "        for i in stones:\r\n",
    "            if i in jewels:\r\n",
    "                res+=1 \r\n",
    "        return res \r\n",
    "        \r\n",
    "        \r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        jewnum = 0\n",
    "        for stone in stones:\n",
    "            if stone in jewels:\n",
    "                jewnum += 1\n",
    "        return jewnum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        L = list(jewels)\n",
    "        L2 = list(stones)\n",
    "        s = 0\n",
    "        for i in range(len(L2)):\n",
    "            if L2[i] in L:\n",
    "                s += 1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numJewelsInStones(self, jewels: str, stones: str) -> int:\n",
    "        jmap = set(jewels)\n",
    "        ret = 0\n",
    "        for s in stones:\n",
    "            if s in jmap:\n",
    "                ret += 1\n",
    "        return ret"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
